﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ImageFan.Forms
{
    internal partial class MainForm
        : Form
    {
        public MainForm()
        {
            InitializeComponent();

            GlobalData.MainFormInstance = this;

            Width = GlobalData.ScreenWidth;
            Height = GlobalData.ScreenHeight;
        }

        #region Private

        private void MainForm_Load(object sender, EventArgs args)
        {
            GetDrivesList();
            SetCurrentDrive();
        }

        private void AssignFolder(string folder)
        {
            tvDirectories.Folder = folder;

            if (tvDirectories.Folder != null)
            {
                flpThumbnails.StopThumbnailsGeneration();
                
                flpThumbnails.CurrentDirectory = tvDirectories.Folder;
                flpThumbnails.ShowThumbnails();
            }
        }

        private void SetCurrentDrive()
        {
            var currentDrive = Environment.CurrentDirectory;
            currentDrive = currentDrive.Substring(0,
                    currentDrive.IndexOf(Path.DirectorySeparatorChar) + 1).ToUpperInvariant();

            cbSelectDrive.SelectedItem = currentDrive;
        }

        private void cbSelectDrive_SelectedIndexChanged(object sender, EventArgs args)
        {
            AssignFolder(cbSelectDrive.Items[cbSelectDrive.SelectedIndex].ToString());
        }

        private void tvDirectories_AfterSelect(object sender, TreeViewEventArgs args)
        {
            if ((tvDirectories.Folder != null) && (Directory.Exists(tvDirectories.Folder)))
            {
                flpThumbnails.CurrentDirectory = tvDirectories.Folder;
                flpThumbnails.ShowThumbnails();
            }
        }

        private void GetDrivesList()
        {
            var drivesList = Environment.GetLogicalDrives();
            for (var i = 0; i < drivesList.Length; i++)
                cbSelectDrive.Items.Add(drivesList[i].ToUpperInvariant());
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs args)
        {
            flpThumbnails.StopThumbnailsGeneration();
        }

        #endregion
    }
}
